package com.baseutils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by XinYi on 2019/5/15.
 * 单纯的文件读写
 */
public class CZFileUtil {
    /**
     * 写文件（默认自动换行）
     * @param fileAbsolutePath
     * @param txt
     * @return
     */
    public static boolean write(String fileAbsolutePath,String txt)
    {
        return write(fileAbsolutePath, txt,true);
    }

    /**
     * 写文件
     * @param fileAbsolutePath
     * @param txt
     * @param appendEnterChar       是否换行
     * @return
     */
    public static boolean write(String fileAbsolutePath,String txt,boolean appendEnterChar){
        FileWriter fw = null;
        try
        {
            File file = new File(fileAbsolutePath);
            File parentFile = file.getParentFile();
            if(!parentFile.exists()){
                parentFile.mkdirs();
            }
            if(!file.exists()){
                Log.d("cz", "createNewFile: " +  file.getAbsolutePath());
                file.createNewFile();
            }
            fw = new FileWriter(fileAbsolutePath, true);
            fw.write(txt);
            if(appendEnterChar){
                fw.write("\r\n");
            }
            fw.flush();
            return true;
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }finally {
            if(fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     * 清空文件
     * @param fileAbsolutePath
     * @return
     */
    public static boolean clear(String fileAbsolutePath) {
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(fileAbsolutePath, false);
            out.write(new String("").getBytes());
            out.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }


    /**
     * 读文件
     * @param fileAbsolutePath
     * @return
     */
    public static StringBuilder read(String fileAbsolutePath) {
        StringBuilder sb = new StringBuilder();
        File file = new File(fileAbsolutePath);
        BufferedReader br = null;
        try {
            try {
                if(file.exists()){
                    br = new BufferedReader(new FileReader(file));
                }else{
                    return sb;
                }
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
            String line = "";
            try {
                if(br == null){
                    return sb;
                }else{
                    while((line = br.readLine())!=null){
                        sb.append(line+"\n");
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb;
    }

    public static void refreshDir(String filePath,Context context){
        File fileDir = new File(filePath);
        listDir(context,fileDir);
    }


    private static void listDir(Context context,File fileDir){
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri uri = Uri.fromFile(fileDir);
        intent.setData(uri);
        context.sendBroadcast(intent);

        if(fileDir.isDirectory()){
            File[] files = fileDir.listFiles();
            if(files != null && files.length > 0){
                for (File file : files){
                    listDir(context,file);
                }
            }
        }
    }
}
